package tmproject.hlhj.fhp.cartrans.fragments

import android.Manifest
import android.content.Intent
import com.bumptech.glide.Glide
import com.huantansheng.easyphotos.EasyPhotos
import com.tbruyelle.rxpermissions.RxPermissions
import kotlinx.android.synthetic.main.aty_regis.*
import kotlinx.android.synthetic.main.fgm_usercenter.*
import kotlinx.android.synthetic.main.fgm_usercenter.view.*
import tmproject.hlhj.fhp.cartrans.BaseFgm
import tmproject.hlhj.fhp.cartrans.IView.HomeView
import tmproject.hlhj.fhp.cartrans.IView.UserCenterVIew
import tmproject.hlhj.fhp.cartrans.R
import tmproject.hlhj.fhp.cartrans.activitys.*
import tmproject.hlhj.fhp.cartrans.customVIews.MyGlideEngin
import tmproject.hlhj.fhp.cartrans.http.Urls
import tmproject.hlhj.fhp.cartrans.javaBean.*
import tmproject.hlhj.fhp.cartrans.log
import tmproject.hlhj.fhp.cartrans.presenter.HomePresenter
import tmproject.hlhj.fhp.cartrans.presenter.UserCenterPresenter
import tmproject.hlhj.fhp.cartrans.utils.GlideUtil
import tmproject.hlhj.fhp.cartrans.utils.SpHelper
import tmproject.hlhj.fhp.cartrans.utils.ToastUtils
import java.io.File

/**
 * Created by Never Fear   on 2018\8\28 0028.
Never More....
 */
class UserCenterFgm :BaseFgm<UserCenterVIew,UserCenterPresenter>(), UserCenterVIew,HomeView {
    private var head: String?=""
    private lateinit var homePresneter:HomePresenter
    override fun initView() {
        val driver = SpHelper.getDriver(context!!)
        val head_pic = driver?.data?.head_pic
        if (head_pic!=null){
            if (head_pic?.contains("uploads")){
                Glide.with(context!!).load(Urls.ImgBase+head_pic).apply(GlideUtil.loadHeadOption()).into(rootView.userIcon)
            }else{
                Glide.with(context!!).load(head_pic).apply(GlideUtil.loadHeadOption()).into(rootView.userIcon)
            }
        }
        log("fhp","1")
    }

    override fun changeHead(baseBean: BaseBean) {
        val driver = SpHelper.getDriver(context!!)
        driver?.data?.head_pic=head
        SpHelper.saveDriver(context!!,driver!!)
        if (head?.contains("uploads")!!){
            Glide.with(context!!).load(Urls.ImgBase+head).apply(GlideUtil.loadHeadOption()).into(rootView.userIcon)
        }else{
            Glide.with(context!!).load(head).apply(GlideUtil.loadHeadOption()).into(rootView.userIcon)
        }
    }

    override fun setLearn(learInfoBean: LearInfoBean) {
    }

    override fun logicStart() {
//        homePresneter.getInfo(context!!,SpHelper.getToken(context!!))
    }

    override fun gotUserInfo(userInfoBean: UserInfoBean) {

    }

    private val TakePhoto: Int=110

    override fun setListener() {
        rootView.btIdentity.setOnClickListener {
            startActivityForResult(Intent(context,IdentityAty::class.java),1)
        }
        rootView.btDrive.setOnClickListener {
            startActivityForResult(Intent(context,DriveCardAty::class.java),1)

        }
        rootView.btZg.setOnClickListener {
            startActivityForResult(Intent(context,ZgAty::class.java),1)

        }
        rootView.btSetting.setOnClickListener {
            startActivityForResult(Intent(context,SettingAty::class.java),1)
        }

        rootView.isSf.setOnClickListener {
            val intent = Intent(context, IdentityAty::class.java)
            startActivityForResult(intent,1)

        }
        rootView.btinfo.setOnClickListener {
            val intent = Intent(context, CarinfoAty::class.java)
            startActivityForResult(intent,1)
        }
        rootView.userIcon.setOnClickListener {
            if (!RxPermissions.getInstance(context).isGranted(Manifest.permission.CAMERA)||!RxPermissions.getInstance(context).isGranted(Manifest.permission.READ_EXTERNAL_STORAGE)){
                RxPermissions.getInstance(context).request(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                        .subscribe { t ->
                            if (!t!!){

                            }else{
                                EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                                        .setFileProviderAuthority("tmproject.hlhj.fhp.fileprovider")//参数说明：见下方`FileProvider的配置`
                                        .start(TakePhoto)
                            }
                        }
            }else{
                EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                        .setFileProviderAuthority("tmproject.hlhj.fhp.fileprovider")//参数说明：见下方`FileProvider的配置`
                        .start(TakePhoto)
            }
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        when(requestCode){
            TakePhoto->{
                val pathList = data?.getStringArrayListExtra(EasyPhotos.RESULT_PATHS)!!
                head=pathList[0]
                presenter?.changeHead(context!!,File(pathList[0]))

            }
        }
    }
    override fun bindView(): UserCenterVIew {
        return this
    }

    override fun onPause() {
        log("fhp","userCenter---2")
        super.onPause()
    }
    override fun onResume() {
        homePresneter.getInfo(context!!,SpHelper.getToken(context!!))
        super.onResume()

    }
    override fun createPresenter(): UserCenterPresenter {
        homePresneter= HomePresenter()
        homePresneter.createModule()
        homePresneter.bindView(this)
        return UserCenterPresenter()
    }

    override fun setDriverInfo(driverInfoBean: DriverInfoBean) {
        log("fhp","设置了?")
        SpHelper.saveDriver(context!!,driverInfoBean)
        rootView.userName.text="姓名：${driverInfoBean?.data.name}"
        rootView.userPhone.text="电话：${driverInfoBean.data.mobile}"
        if (driverInfoBean.data.is_sm_authenticated.equals("0")){
            rootView.isSf.setText("未认证")
        }else{
            rootView.isSf.setText("已认证")
        }

        if (driverInfoBean.data.is_jsz_authenticated.equals("0")){
            rootView.isJs.setText("未认证")
        }else{
            rootView.isJs.setText("有效期至 ${driverInfoBean.data.jsz_expire_date}")
        }
        if (driverInfoBean.data.is_zgz_authenticated.equals("0")){
            rootView.isZg.setText("未认证")
        }else{
            rootView.isZg.setText("有效期至${driverInfoBean.data.zgz_expire_date}")
        }
    }

    /**
     * 无用
     */
    override fun setUnReadNum(unReadNumBean: UnReadNumBean) {

    }

    override fun resum() {

    }
    override fun getContentId(): Int {
        return R.layout.fgm_usercenter
    }
}